(function($, doc) {
$.plusReady(function() {
	var user = app.getState();
	var myWallet = ethscan.getWallet();
	//console.log(JSON.stringify(myWallet));
	//STVEU8NV8UAEKRI47485NPKY3CBVDIKRKI
	//AppName: gatc
	//console.log(JSON.stringify(user));
	
	doc.getElementById('weep').addEventListener('tap', function() {
		plus.webview.open('pages/find/weep.html', 'weep');
	});
	doc.getElementById('mgrwallet').addEventListener('tap', function() {
		$.openWindow({
			url: 'pages/coin/mgrwallet.html',
			id: 'mgrwallet'
		});
	});
	doc.getElementById('qrcode').addEventListener('tap', function() {
		plus.webview.close('qrcode','none');
		$.openWindow({
			url: 'pages/coin/qrcode.html',
			id: 'qrcode'
		});
	});
	//ethscan.setEth();
	//localStorage.clear();
	if(!myWallet) {
		$.alert('请创建默认钱包！');
		$.openWindow({
			url: 'pages/coin/mgrwallet.html',
			id: 'mgrwallet'
		});
	}
	var flushwalletlist = function() {
		ethscan.getmyCoin(function(data) {
			//console.log(JSON.stringify(data));
			/*
			return;
			
			*/
			doc.getElementById('wallet-list').innerHTML = template('wallet-template', {
				data: data
			});
			var list = [];
			//var provider = ethers.providers.getDefaultProvider('homestead');
		
			for(var p in data) {
				var item = data[p];
				/*
				var addr = item['address'];
				console.log(item['ctype']);
				console.log(addr);
				
				if(item['ctype'] == 'eth') {
					provider.getBalance(user.eth_address).then(function(balance) {
						var etherString = ethers.utils.formatEther(balance);
						console.log(etherString);
						user.eth_money = etherString;
						app.setState(user);
						doc.getElementById('eth_money').innerHTML = user.eth_money;
						//doc.getElementById("eth_money").innerHTML = etherString;
					});
					
				}
				else if(item['ctype'] == 'ethcontact') {
					//doc.getElementById(addr + 'money') = user.eth_money;
					var contracts = new ethers.Contract(addr, item['abi'], provider);

					contracts.balanceOf(user.eth_address).then(function(res) {
						//item['money'] = res["_bn"];
						var etherString = ethers.utils.formatEther(res);
						console.log(JSON.stringify(etherString));
						doc.getElementById(addr + '_money').innerHTML = etherString;
					});
				}else{
					doc.getElementById(addr + '_money').innerHTML = 0;
				}*/
				list.push(item['name']);
			}
			doc.getElementById('coinnum').innerHTML = list.length;
			//console.log(apiurl + 'eth/get_price?coin='+list.join(","));
			$.getJSON(apiurl + 'eth/get_price?coin='+list.join(","),function(res){
				//console.log(JSON.stringify(res));
				if(res){
					//res['GATC'] = 1.5;
					console.log(JSON.stringify(res));
					localStorage.setItem('coinprice', JSON.stringify(res));
					for(var p in res) {
						var priceel = doc.getElementById(p + '_price');
						if(priceel) {
							priceel.innerHTML = '≈$' + res[p];
						}
						//priceel.parentNode.setAttribute('data-price',res[p]);
					}
				}
			});
		});
	};
	flushwalletlist();
	window.addEventListener('flashWallet', function(event) {
		flushwalletlist();
	});
	
	$("#wallet-list").on("tap", "a",function() {
		//console.log(JSON.stringify(this));
		//plus.webview.close('coindetail','none');
		$.openWindow({
			url : 'pages/coin/coindetail.html',
			id : 'coindetail',
			extras : {
				address : this.id,
				name : this.getAttribute('data-name')
			},
			createNew : true
		})
	}, false);
	//var provider = ethers.providers.getDefaultProvider();
});
}(mui, document));